package com.itheima.aop;

import com.alibaba.fastjson.JSONObject;
import com.itheima.pojo.Result;
import com.itheima.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@Slf4j
@Aspect
public class PowerAspect {
    @Autowired
    private HttpServletRequest request;

    @Around("@annotation(com.itheima.anno.Power)")
    private Object power(ProceedingJoinPoint pjp) throws Throwable {
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        Integer type = (Integer) claims.get("type");
        if (type == 0) {
            return Result.error("没有权限");
        }
        Object result = pjp.proceed();
        return result;
    }
}
